<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="内容ID" prop="contentId">
        <el-input
          v-model="queryParams.contentId"
          placeholder="请输入内容ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="内容类型" prop="contentType">
        <el-select v-model="queryParams.contentType" placeholder="请选择内容类型" clearable>
          <el-option
            v-for="dict in dict.type.bole_content_category_category_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="内容类型" prop="contentType">
  <el-select v-model="queryParams.contentType" placeholder="请选择内容类型" clearable>
    <el-option label="新闻" value="1" />
    <el-option label="活动" value="2" />
    <el-option label="公告" value="3" />
    <!-- <el-option label="音频" value="audio" />
    <el-option label="直播" value="live" /> -->
  </el-select>
</el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="分类ID" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="请选择分类ID" clearable>
          <el-option
            v-for="dict in dict.type.bole_content_category_category_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="活动时间" prop="eventTime">
        <el-date-picker clearable
          v-model="queryParams.eventTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择活动时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="活动地点" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入活动地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="报名截止时间" prop="regEndTime">
        <el-date-picker clearable
          v-model="queryParams.regEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报名截止时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="最大报名人数" prop="maxAttendees">
        <el-input
          v-model="queryParams.maxAttendees"
          placeholder="请输入最大报名人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="是否需要审核" prop="needAudit">
        <el-input
          v-model="queryParams.needAudit"
          placeholder="0不需要 1需要"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bole:content:add']"
        >新增</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddAnnounce"
          v-hasPermi="['bole:content:add']"
        >新增公告</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bole:content:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bole:content:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bole:content:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="内容ID" align="center" prop="contentId" /> -->
      <!-- <el-table-column label="内容类型" align="center" prop="contentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bole_content_category_category_type" :value="scope.row.contentType"/>
        </template>
      </el-table-column> -->
      <el-table-column label="内容类型" align="center" prop="contentType">
  <template slot-scope="scope">
    <span v-if="scope.row.contentType === '1'">新闻</span>
    <span v-else-if="scope.row.contentType === '2'">活动</span>
    <span v-else-if="scope.row.contentType === '3'">公告</span>
    <span v-else>{{ scope.row.contentType }}</span>
  </template>
</el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="封面图路径" align="center" prop="cover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="富文本内容" align="center" prop="content" show-overflow-tooltip/>
      <!-- <el-table-column label="活动时间" align="center" prop="eventTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eventTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="活动地点" align="center" prop="location" /> -->
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
       <el-table-column label="报名所需积分" align="center" prop="requiredIntegral" />
      <el-table-column label="报名截止时间" align="center" prop="regEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.regEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="最大报名人数" align="center" prop="maxAttendees" /> -->
      <!-- <el-table-column label="是否需要审核" align="center" prop="needAudit" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bole:content:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bole:content:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改内容信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="内容类型" prop="contentType" v-if="form.contentType !=3">
          <el-select v-model="form.contentType" placeholder="请选择内容类型">
            <el-option
              v-for="dict in dict.type.bole_content_category_category_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item  v-if="form.contentType !=3" label="封面图路径(1张)" prop="cover">
          <image-upload v-model="form.cover"/>
        </el-form-item>
        <!-- <el-form-item label="分类ID" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择分类ID">
            <el-option
              v-for="dict in dict.type.bole_content_category_category_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item> -->

         <el-form-item v-if="form.contentType == 2" label="报名所需积分" prop="requiredIntegral">
          <el-input v-model="form.requiredIntegral" type="text" placeholder="报名所需积分" />
        </el-form-item>
        <el-form-item v-if="form.contentType == 1 || form.contentType == 2" label="富文本内容(必填)">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item v-if="form.contentType == 3" label="公告" prop="remark">
          <el-input v-model="form.content" type="textarea" placeholder="请输入公告内容" />
        </el-form-item>
        <!-- <el-form-item label="活动时间" prop="eventTime">
          <el-date-picker clearable
            v-model="form.eventTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="活动地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入活动地点" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="报名截止时间" prop="regEndTime">
          <el-date-picker clearable
            v-model="form.regEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名截止时间">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="最大报名人数" prop="maxAttendees">
          <el-input v-model="form.maxAttendees" placeholder="请输入最大报名人数" />
        </el-form-item> -->
        <!-- <el-form-item label="是否需要审核(活动报名)" prop="needAudit">
      <el-select v-model="form.needAudit" placeholder="请选择是否需要审核">
        <el-option label="是" value="1"></el-option>
        <el-option label="否" value="0"></el-option>
      </el-select>
    </el-form-item> -->
        <el-divider content-position="center" v-if="form.contentType !=3">顶部轮播图图片信息</el-divider>
        <el-row :gutter="10" class="mb8" v-if="form.contentType !=3">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBoleContentImage">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBoleContentImage">删除</el-button>
          </el-col>
        </el-row>

        <el-table v-if="form.contentType !=3" :data="boleContentImageList" :row-class-name="rowBoleContentImageIndex" @selection-change="handleBoleContentImageSelectionChange" ref="boleContentImage">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>

          <el-table-column label="图片地址" prop="sortNum" width="300">
            <template slot-scope="scope">
              <el-form-item label="" prop="imgUrl">
                <image-upload v-model="scope.row.imgUrl"/>
              </el-form-item>
            </template>
          </el-table-column>

          <!-- <el-table-column label="排序号" prop="sortNum" width="50">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sortNum" placeholder="请输入排序号" />
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent } from "@/api/bole/content";

export default {
  name: "Content",
  dicts: ['bole_content_category_category_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBoleContentImage: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 内容信息表格数据
      contentList: [],
      // 内容图片表格数据
      boleContentImageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contentId: null,
        contentType: null,
        title: null,
        cover: null,
        categoryId: null,
        content: null,
        eventTime: null,
        location: null,
        regEndTime: null,
        maxAttendees: null,
        needAudit: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contentType: [
          { required: true, message: "内容类型不能为空", trigger: "change" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询内容信息列表 */
    getList() {
      this.loading = true;
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contentId: null,
        contentType: null,
        title: null,
        cover: null,
        categoryId: null,
        userId: null,
        deptId: null,
        content: null,
        eventTime: null,
        location: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        regEndTime: null,
        maxAttendees: null,
        needAudit: null
      };
      this.boleContentImageList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.form.contentType = 1;
      this.open = true;
      this.title = "添加内容信息";
    },
    handleAddAnnounce() {
      this.reset();
      this.content=null;
      this.form.contentType = 3;
      this.form.title="最新公告";
      this.open = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contentId = row.contentId || this.ids
      getContent(contentId).then(response => {
        this.form = response.data;
        this.boleContentImageList = response.data.boleContentImageList;
        this.open = true;
        this.title = "修改内容信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.cover != null){
        if(this.form.cover.includes(',')){
        this.$modal.msgError("封面最多上传1张");
        return
      }
      }

      // 如果内容为空不行
      if(this.form.content == null || this.form.content.trim() == "" || this.form.content == "<p><br></p>"){
        this.$modal.msgError("内容不能为空");
        return
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.boleContentImageList = this.boleContentImageList;
          if (this.form.contentId != null) {
            updateContent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contentIds = row.contentId || this.ids;
      this.$modal.confirm('是否确认删除内容信息编号为"' + contentIds + '"的数据项？').then(function() {
        return delContent(contentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 内容图片序号 */
    rowBoleContentImageIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 内容图片添加按钮操作 */
    handleAddBoleContentImage() {
      let obj = {};
      obj.imgUrl = "";
      obj.sortNum = "";
      if(this.boleContentImageList.length <1){
        this.boleContentImageList.push(obj);
      }

    },
    /** 内容图片删除按钮操作 */
    handleDeleteBoleContentImage() {
      if (this.checkedBoleContentImage.length == 0) {
        this.$modal.msgError("请先选择要删除的内容图片数据");
      } else {
        const boleContentImageList = this.boleContentImageList;
        const checkedBoleContentImage = this.checkedBoleContentImage;
        this.boleContentImageList = boleContentImageList.filter(function(item) {
          return checkedBoleContentImage.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBoleContentImageSelectionChange(selection) {
      this.checkedBoleContentImage = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bole/content/export', {
        ...this.queryParams
      }, `content_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
